// 二叉树的右视图
package Leetcode;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

public class Solution199 {
    public static List<Integer> rightSideView(TreeNode root) {
        if (root == null)
            return new ArrayList<Integer>();
        Deque<TreeNode> queue = new LinkedList<>();
        List<Integer> result = new ArrayList<>();
        queue.push(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            System.out.println(size);
            while (size-- != 0) {
                TreeNode t = queue.pop();
                if (t.left != null)
                    queue.addLast(t.left);
                if (t.right != null)
                    queue.addLast(t.right);
                if (size == 0)
                    result.add(t.val);
            }
        }
        return result;
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        TreeNode root1 = new TreeNode(2);
        TreeNode root2 = new TreeNode(3);
        TreeNode root3 = new TreeNode(5);
        TreeNode root4 = new TreeNode(4);
        root.left = root1;
        root.right = root2;
        root1.right = root3;
        root2.right = root4;
        rightSideView(root);
    }
}
